package com.wish.g1.accounting.controller;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g1.accounting.entity.DealPerDay;
import com.wish.g1.accounting.entity.WishDealPerDay;
import com.wish.g1.accounting.service.IDealPerDayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wish.g1.common.log.annotation.Log;
import com.wish.g1.common.log.enums.BusinessType;
import com.wish.g1.common.security.annotation.RequiresPermissions;
import com.wish.g1.common.core.web.controller.BaseController;
import com.wish.g1.common.core.web.domain.AjaxResult;
import com.wish.g1.common.core.utils.poi.ExcelUtil;
import com.wish.g1.common.core.web.page.TableDataInfo;

/**
 * wish财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)Controller
 *
 * @author wish
 * @date 2022-11-21
 */
@RestController
@RequestMapping("/day")
public class WishDealPerDayController extends BaseController
{
    @Autowired
    private IDealPerDayService DealPerDayService;

    /**
     * 查询wish财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)列表
     */
    @RequiresPermissions("accounting:day:list")
    @GetMapping("/list")
    public TableDataInfo list(DealPerDay dealPerDay)
    {
        startPage();
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.setEntity(dealPerDay);
        if (dealPerDay.getOperationType()!=null){
            wrapper.eq("operation_type",dealPerDay.getOperationType());
        }
        if (dealPerDay.getDealDate()!=null){
            System.err.println(dealPerDay.getDealDate());
            wrapper.eq("deal_date",dealPerDay.getDealDate());
        }
        wrapper.orderByAsc("deal_date");
        List<WishDealPerDay> list = DealPerDayService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 导出wish财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)列表
     */
    @RequiresPermissions("accounting:day:export")
    @Log(title = "wish财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DealPerDay dealPerDay)
    {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.setEntity(dealPerDay);
        List<DealPerDay> list = DealPerDayService.list(wrapper);
        ExcelUtil<DealPerDay> util = new ExcelUtil<DealPerDay>(DealPerDay.class);
        util.exportExcel(response, list, "wish财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)数据");
    }

    /**
     * 获取wish财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)详细信息
     */
    @RequiresPermissions("accounting:day:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(DealPerDayService.getById(id));
    }

    /**
     * 新增wish财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)
     */
    @RequiresPermissions("accounting:day:add")
    @Log(title = "wish财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DealPerDay dealPerDay)
    {
        dealPerDay.setCreateTime(LocalDateTime.now());
        return toAjax(DealPerDayService.save(dealPerDay));
    }

    /**
     * 修改wish财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)
     */
    @RequiresPermissions("accounting:day:edit")
    @Log(title = "wish财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DealPerDay dealPerDay)
    {
        dealPerDay.setUpdateTime(LocalDateTime.now());
        return toAjax(DealPerDayService.saveOrUpdate(dealPerDay));
    }

    /**
     * 删除wish财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)
     */
    @RequiresPermissions("accounting:day:remove")
    @Log(title = "wish财务报(批处理对线上收入每天分类汇总统计一条记录，目前只有线上收入)", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(DealPerDayService.removeByIds(Arrays.asList(ids)));
    }
}
